package com.campus.customer.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.campus.customer.entity.Goods;
import com.campus.customer.entity.GoodsSpecGroup;
import com.campus.customer.entity.GoodsSpecOption;
import com.campus.customer.entity.OrderGoods;
import com.campus.customer.mapper.GoodsMapper;
import com.campus.customer.mapper.GoodsSpecGroupMapper;
import com.campus.customer.mapper.GoodsSpecOptionMapper;
import com.campus.customer.vo.GoodsVo;
import com.campus.customer.vo.Option;
import com.campus.customer.vo.OrderGoodsVo;
import com.campus.customer.vo.Specification;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品表 服务实现类
 * </p>
 *
 * @author ZhaoYuJie
 * @since 2025-04-02
 */
@Service
public class GoodsService extends ServiceImpl<GoodsMapper, Goods> {
    @Autowired
    private GoodsSpecGroupMapper goodsSpecGroupMapper;
    @Autowired
    private GoodsSpecOptionMapper goodsSpecOptionMapper;
    @Autowired
    private OrderGoodsService orderGoodsService;


    public List<GoodsVo> listWithSpecifications(Integer merchantId) {
        LambdaQueryWrapper<Goods> queryWrapper = Wrappers.lambdaQuery(Goods.class)
                .eq(Goods::getMerchantId, merchantId)
                .eq(Goods::getStatus, true);

        List<Goods> goodsList = list(queryWrapper);

        return convertGoodsToVoWithSpecifications(goodsList);
    }

    private List<GoodsVo> convertGoodsToVoWithSpecifications(List<Goods> goodsList) {
        if (CollectionUtil.isEmpty(goodsList)) {
            return Collections.emptyList();
        }
        List<GoodsVo> goodsVoList = BeanUtil.copyToList(goodsList, GoodsVo.class);

        Map<Integer, GoodsVo> goodsMap = new HashMap<>();
        for (GoodsVo goods : goodsVoList) {
            goodsMap.put(goods.getId(), goods);
        }

        List<GoodsSpecGroup> specGroups = goodsSpecGroupMapper.selectList(Wrappers.lambdaQuery(GoodsSpecGroup.class).in(GoodsSpecGroup::getGoodsId, goodsMap.keySet()));
        Map<Integer, List<GoodsSpecGroup>> specGroupMap = new HashMap<>();
        for (GoodsSpecGroup specGroup : specGroups) {
            specGroupMap.computeIfAbsent(specGroup.getGoodsId(), k -> new ArrayList<>()).add(specGroup);
        }

        List<GoodsSpecOption> specOptions = new ArrayList<>();
        if (!specGroups.isEmpty()) {
            specOptions = goodsSpecOptionMapper.selectList(Wrappers.lambdaQuery(GoodsSpecOption.class).in(GoodsSpecOption::getSpecGroupId, specGroups.stream().map(GoodsSpecGroup::getId).collect(Collectors.toList())));
        }
        Map<Integer, List<GoodsSpecOption>> specOptionMap = new HashMap<>();
        for (GoodsSpecOption specOption : specOptions) {
            specOptionMap.computeIfAbsent(specOption.getSpecGroupId(), k -> new ArrayList<>()).add(specOption);
        }

        for (GoodsVo goods : goodsVoList) {
            List<Specification> specifications = new ArrayList<>();
            for (GoodsSpecGroup specGroup : specGroupMap.getOrDefault(goods.getId(), new ArrayList<>())) {
                Specification specification = new Specification();
                specification.setId(specGroup.getId());
                specification.setName(specGroup.getName());
                specification.setOptions(specOptionMap.getOrDefault(specGroup.getId(), new ArrayList<>()).stream().map(option -> {
                    Option opt = new Option();
                    opt.setId(option.getId());
                    opt.setName(option.getName());
                    opt.setPrice(option.getPrice().doubleValue());
                    return opt;
                }).collect(Collectors.toList()));
                specifications.add(specification);
            }
            goods.setSpecifications(specifications);
        }

        return goodsVoList;
    }

    public List<OrderGoodsVo> listByOrderId(String orderId) {
        List<OrderGoods> orderGoodsList = orderGoodsService.getOrderGoodsByOrderId(orderId);
        List<Integer> goodsIdList = orderGoodsList.stream().map(OrderGoods::getGoodsId).collect(Collectors.toList());
        List<Goods> goodsList = listByIds(goodsIdList);
        if (CollectionUtil.isEmpty(goodsList)) {
            return Collections.emptyList();
        }
        List<OrderGoodsVo> collect = goodsList.stream().map(goods -> {
            OrderGoods orderGoods = orderGoodsList.stream().filter(order -> order.getGoodsId().equals(goods.getId())).findFirst().orElse(null);
            OrderGoodsVo orderGoodsVo = new OrderGoodsVo();
            orderGoodsVo.setGoods(goods);
            orderGoodsVo.setOrderGoods(orderGoods);
            return orderGoodsVo;
        }).collect(Collectors.toList());
        return collect;
    }
}

